package org.maiyoframework.core.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 生成图片验证码类
 * 
 * @author maiYo
 * 
 */
public class Captcha {

	private static final int WIDTH = 120;
	private static final int HEIGHT = 40;
	private static final int FONTSIZE = 28;
	private static final int WIDTH_LITE = 80;
	private static final int HEIGHT_LITE = 24;
	private static final int FONTSIZE_LITE = 20;
	private static final int LENGTH = 5;
	private final static Random random = new Random();

	/**
	 * 生成验证码图片
	 * 
	 * @param request
	 * @param response
	 * @param width
	 *            长度
	 * @param height
	 *            宽度
	 * @param fontSize
	 *            字号
	 * @throws IOException
	 */
	public static void get(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/png");
		BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
		// 设置背景颜色
		g.setColor(Color.WHITE);
		// 填充矩形
		g.fillRect(0, 0, WIDTH, HEIGHT);
		// 产生干扰元素,随机画10个圆
		for (int i = 0; i < 10; i++) {
			g.setColor(getRandomColor(150, 250));
			g.drawOval(random.nextInt(WIDTH - 10), random.nextInt(HEIGHT - 16),
					5 + random.nextInt(10), 5 + random.nextInt(10)); // 应用随机颜色进行随机画圆
		}
		// 产生干扰元素,随机画1条直线
		g.setColor(getRandomColor(50, 200));
		g.drawLine(10 + random.nextInt(20), 5 + random.nextInt(HEIGHT / 2),
				WIDTH - 30 + random.nextInt(20),
				10 + random.nextInt(HEIGHT / 2));
		// 产生随机验证码
		Font font = new Font("Arial", Font.ITALIC, FONTSIZE);
		g.setFont(font);
		g.setColor(getRandomColor(10, 240));
		// String code = getRandomCode();
		String code = getRandomAlphanumeric(LENGTH);
		g.drawString(code, 10, 30);
		request.getSession().setAttribute("captcha", code);
		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(bufferedImage, "png", out);
		out.close();
	}

	/**
	 * 生成小尺寸验证码图片
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public static void getLite(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/png");
		BufferedImage bufferedImage = new BufferedImage(WIDTH_LITE,
				HEIGHT_LITE, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
		// 设置背景颜色
		g.setColor(Color.WHITE);
		// 填充矩形
		g.fillRect(0, 0, WIDTH_LITE, HEIGHT_LITE);
		// 产生干扰元素,随机画10个圆
		for (int i = 0; i < 10; i++) {
			g.setColor(getRandomColor(150, 250));
			g.drawOval(random.nextInt(WIDTH_LITE - 10),
					random.nextInt(HEIGHT_LITE - 16), 5 + random.nextInt(10),
					5 + random.nextInt(10)); // 应用随机颜色进行随机画圆
		}
		// 产生干扰元素,随机画1条直线
		g.setColor(getRandomColor(50, 200));
		g.drawLine(10 + random.nextInt(20),
				5 + random.nextInt(HEIGHT_LITE / 2),
				WIDTH_LITE - 30 + random.nextInt(20),
				10 + random.nextInt(HEIGHT_LITE / 2));
		// 产生随机验证码
		Font font = new Font("Arial", Font.ITALIC, FONTSIZE_LITE);
		g.setFont(font);
		g.setColor(getRandomColor(10, 240));
		// String code = getRandomCode();
		String code = getRandomAlphanumeric(LENGTH);
		g.drawString(code, 5, 20);
		request.getSession().setAttribute("captcha", code);
		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(bufferedImage, "png", out);
		out.close();
	}

	// /**
	// * 获取指定长度随机码
	// *
	// * @return code
	// */
	// private static String getRandomCode() {
	// String code = RandomStringUtils.randomAlphanumeric(LENGTH)
	// .toUpperCase();
	// return code;
	// }

	/**
	 * 获取指定长度随机码
	 * 
	 * @param length
	 *            长度
	 * @return
	 */
	private static String getRandomAlphanumeric(int length) {
		char ch = '0';
		String code = "";
		for (int i = 0; i < length; i++) {
			int flag = random.nextInt(3);
			if (flag == 0) {
				ch = (char) (48 + random.nextInt(10));
			} else {
				ch = (char) (65 + random.nextInt(26));
			}
			code += ch;
		}
		return code;
	}

	/**
	 * 获取给定范围随机颜色
	 * 
	 * @param i
	 * @param j
	 * @return Color
	 */
	private static Color getRandomColor(int i, int j) {
		if (i > 255)
			i = 255;
		if (j > 255)
			j = 255;
		int r = i + random.nextInt(j - i);
		int g = i + random.nextInt(j - i);
		int b = i + random.nextInt(j - i);
		return new Color(r, g, b);
	}

	/**
	 * 验证码核实
	 * 
	 * @param sessionCode
	 * @param verifyCode
	 * @return
	 */
	public static boolean verify(String sessionCode, String verifyCode) {
		if (verifyCode != null) {
			return sessionCode.equalsIgnoreCase(verifyCode);
		}
		return false;
	}

}
